﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransCend.DAL;
using TransCend.DB;
using TransCend.Model;
using TransCend.Util.Page;

namespace TransCend.Service
{
    public abstract class BaseService<T> : IBaseService<T> where T : BaseModel
    {
        public abstract IBaseDAL<T> GetBaseDAL();

        public virtual T FindByID(int id)
        {
            return GetBaseDAL().FindByID(id);
        }

        public virtual T FindByProperty(string propertyname, object propertyvalue)
        {
            return GetBaseDAL().FindByProperty(propertyname, propertyvalue);
        }

        public virtual T FindByPropertys(string[] propertyname, string[] propertyvalue)
        {
            return GetBaseDAL().FindByPropertys(propertyname, propertyvalue);
        }

        public virtual int Insert(T model)
        {
            return GetBaseDAL().Insert(model);
        }

        public int InsertReturnId(T model)
        {
            return GetBaseDAL().InsertReturnId(model);
        }

        public virtual int Update(T model)
        {
            return GetBaseDAL().Update(model);
        }

        public virtual int Merge(T model)
        {
            return GetBaseDAL().Merge(model);
        }

        public virtual int Delete(int id)
        {
            return GetBaseDAL().Delete(id);
        }

        public virtual int Delete(int[] ids)
        {
            return GetBaseDAL().Delete(ids);
        }

        /// <summary>
        /// 所有数据
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> FindAll(params OrderBy[] orderBy)
        {
            return GetBaseDAL().FindAll(orderBy);
        }

        public IList<T> FindList(T queryModel, params OrderBy[] orderBy)
        {
            return GetBaseDAL().FindList(queryModel, orderBy);
        }

        public IList<T> FindList(int[] ids, params OrderBy[] orderBy)
        {
            return GetBaseDAL().FindList(ids, orderBy);
        }

        public virtual PageTools<T> FindPage(int page, int rows, OrderBy[] orderBy)
        {
            return GetBaseDAL().FindPage(page, rows, orderBy);
        }

        public PageTools<T> FindPage(int page, int rows, T queryModel, OrderBy[] orderBy)
        {
            return GetBaseDAL().FindPage(page, rows, queryModel, orderBy);
        }

        public OrderBy[] GetModelOrderBy(T m)
        {
            string sort = m.Sort;
            OrderBy[] orderBy = null;
            if (!string.IsNullOrEmpty(sort))
            {
                string[] sorts = sort.Split(',');
                string[] orders = m.Order.Split(',');
                orderBy = new OrderBy[sorts.Length];
                for (int i = 0; i < sorts.Length; i++ )
                {
                    string sort0 = sorts[i];
                    orderBy[i] = new OrderBy(sort0, orders[i]);
                }
            }
            return orderBy;
        }


        public virtual DateTime FindNowDate()
        {
            return GetBaseDAL().FindNowDate();
        }

        public int FindListCount(T queryModel)
        {
            return GetBaseDAL().FindListCount(queryModel);
        }
    }
}
